package cs.sy.service.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cs.sy.service.bean.UserBean;
import cs.sy.service.business.UserLoginBuss;

/**
 * 作者： sunhouji
 * 时间： 2011-8-4 下午12:43:59
 */
public class UserLoginServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;

	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try{
		if(req.getParameter("des") != null && req.getParameter("des").equals("0")){
			req.getSession().setAttribute("user", null);
			req.getRequestDispatcher("/u_login.jsp").forward(req, resp);
		}else{
			String name = req.getParameter("user");
			String pass = req.getParameter("pass");
			UserBean userBean = new UserLoginBuss().getUserByNamePass(name, pass);
			if(userBean != null){
				req.getSession().setAttribute("user", userBean);
				req.setAttribute("food",new UserLoginBuss().getAllFood());
				req.getRequestDispatcher("/food.jsp").forward(req, resp);
			}else{
				req.setAttribute("result", "登录失败...");
				req.getRequestDispatcher("/u_login.jsp").forward(req, resp);
			}
		}
		}catch (Exception e) {
			req.setAttribute("result", "内部错误,登录失败...");
			req.getRequestDispatcher("/u_login.jsp").forward(req, resp);
		}
	}

}

